package cn.doitedu.day05

import org.apache.spark.{SparkConf, SparkContext, TaskContext}

import java.net.InetAddress

object T05_SerTest02 {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName(this.getClass.getSimpleName)
    val sc = new SparkContext(conf)

    val lines = sc.textFile(args(0))

    //RulesMapClassSer实例是在Driver端创建的
    val rulesMapClassSer = new RulesMapClassSer()
    println(rulesMapClassSer)

    lines.map(line => {
      val fields = line.split(",")
      val cid = fields(0).toInt
      val money = fields(1).toDouble
      val name = rulesMapClassSer.rules.getOrElse(cid, "未知")
      //获取当前程序执行时对应的主机名
      val hostname = InetAddress.getLocalHost.getHostName
      //获取当前Task的分区编号
      val partitionId = TaskContext.getPartitionId()
      //获取当前线程Id
      val threadId = Thread.currentThread().getId
      (cid, name, money, hostname, partitionId, threadId, rulesMapClassSer)
    }).saveAsTextFile(args(1))

    sc.stop()
  }

}
